#include<bits/stdc++.h>
using uint=unsigned int;
using ll=long long;
using ull=unsigned long long;
using pii=std::pair<int,int>;
#define fst first
#define scd second
#define all(x) std::begin(x),std::end(x)
#define y0 y000000
#define y1 y111111
#define rep(i,s,t) for(int i=(s);i<=(t);i++)
#define irep(i,s,t) for(int i=(s);i>=(t);i--)
template<typename T> bool chmax(T &x,T const &y){
    return x<y?x=y,true:false;
}
template<typename T> bool chmin(T &x,T const &y){
    return y<x?x=y,true:false;
}
using namespace std;
template<typename T=int>
T read(){
    T x=0,f=1;
    char c=getchar();
    while(!isdigit(c)){
        f=c=='-'?-1:1;
        c=getchar();
    }
    while(isdigit(c)){
        x=x*10+(c-'0');
        c=getchar();
    }
    return x*f;
}
void work(){
    int n;cin>>n;
    vector<int> a;
    ll ans0=0;
    rep(i,0,n-1){
        int x;cin>>x;
        if(!a.empty()&&a.back()==x) ans0+=x;
        else a.push_back(x);
    }
    n=a.size();
    a.push_back(0);
    vector<ll> f(n,0);
    vector<int> lst(1e6+1,-1);
    rep(i,0,n-1){
        if(i){
            chmax(f[i],f[i-1]);
        }
        if(lst[a[i+1]]!=-1){
            chmax(f[i],f[lst[a[i+1]]]+a[i+1]);
        }
        lst[a[i]]=i;
    }
    cout<<f[n-1]+ans0<<'\n';
}
signed main(){
    freopen("color.in","r",stdin);
    freopen("color.out","w",stdout);
    ios::sync_with_stdio(false);
    int T;cin>>T;
    while(T--) work();
}